package graphics;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import model.Ressources;

public class GRessources extends JPanel implements Observer {
    
    /** Variables de classe */
    final public static int largeur = 128, hauteur = 40;

    /** Attributs */
    private Ressources m_ressources;
    private List<JLabel> m_labelList;
    private GridBagConstraints m_gridBagConstraints;

    /** Constructeurs */
    public GRessources(Ressources ressources) {
        super(new GridBagLayout());

        m_ressources = ressources;
        m_ressources.addObserver(this);
        m_labelList = new LinkedList<JLabel>();

        m_gridBagConstraints = new GridBagConstraints();
        m_gridBagConstraints.insets = new Insets(0,10,0,10);
        m_gridBagConstraints.gridx = 0;
        m_gridBagConstraints.gridy = GridBagConstraints.RELATIVE;

        update();
    }

    /** Methodes */
    public void update() {
        List<JLabel> labelToAdd = new LinkedList<JLabel>();
        Iterator<JLabel> iterLabel = m_labelList.iterator();
        for (Ressources.Type key : Ressources.Type.values()) {
            if (m_ressources.isAvailable(key)) {
                String text = key + " : " + (int)m_ressources.getQuantity(key);
                if (m_ressources.isLimited(key)) text += " / " + (int)m_ressources.getLimit(key);
                if (iterLabel.hasNext()) iterLabel.next().setText(text);
                else {
                    JLabel label = new JLabel(text);
                    add(label, m_gridBagConstraints);
                    labelToAdd.add(label);
                }
            }
        }
        while (iterLabel.hasNext()) { iterLabel.remove(); }
        for (JLabel label : labelToAdd) m_labelList.add(label);
    }
        
    public void update(Observable economy, Object economyUpdate) { update(); }
}